/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.network;

import client.controller.events.VisitorCreatedEvent;
import client.controller.events.VisitorDeletedEvent;
import client.controller.events.VisitorGuideEvent;
import client.controller.events.VisitorListener;
import client.controller.events.VisitorUpdatedEvent;
import java.util.LinkedList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author nicolas
 */
public class NetworkEventsDispatcher {
    //Variables declaration

    private static NetworkEventsDispatcher instance = null;
    private List<VisitorListener> visitorListeners;
    final Logger logger = LoggerFactory.getLogger(NetworkEventsDispatcher.class);
    
    public synchronized static NetworkEventsDispatcher getApplicationEventsDispatcher() {
        if (instance == null) {
            instance = new NetworkEventsDispatcher();
        }
        return instance;
    }

    private NetworkEventsDispatcher() {
        visitorListeners = new LinkedList<VisitorListener>();
    }

    public void addVisitorListener(VisitorListener listener) {
        visitorListeners.add(listener);
    }

    public void removeVisitorListener(VisitorListener listener) {
        visitorListeners.remove(listener);
    }

    public void fireVisitorCreatedEvent(VisitorCreatedEvent evt) {
        for (VisitorListener l : visitorListeners) {
            l.visitorCreated(evt);
            logger.debug("VisitorCreatedEvent fired from {} : {}",evt.getSource().getClass().getSimpleName(),evt.getVisitor());
        }
    }

    public void fireVisitorUpdatedEvent(VisitorUpdatedEvent evt) {
        for (VisitorListener l : visitorListeners) {
            l.visitorUpdated(evt);
            logger.debug("VisitorUpdatedEvent fired from {} : {}",evt.getSource().getClass().getSimpleName(),evt.getVisitor());
        }
    }

    public void fireVisitorDeletedEvent(VisitorDeletedEvent evt) {
        for (VisitorListener l : visitorListeners) {
            l.visitorDeleted(evt);
            logger.debug("VisitorDeletedEvent fired from {} : {}",evt.getSource().getClass().getSimpleName(),evt.getVisitor());
        }
    }

    public void fireVisitorGuideEvent(VisitorGuideEvent evt) {
        for (VisitorListener l : visitorListeners) {
            l.visitorGuide(evt);
            logger.debug("VisitorGuideEvent fired from {} : {}",evt.getSource().getClass().getSimpleName(),evt.isGuide());
        }
    }
}
